package in.muton.edushare.components.autocomplete;

import in.muton.edushare.components.autocomplete.api.MainInviter;
import in.muton.edushare.newentities.upgraded.InviteFactory;
import in.muton.edushare.newentities.upgraded.RoleRenderer;
import in.muton.edushare.newentities.upgraded.multipleInvite.MultipleAudienceInvite;
import in.muton.edushare.newentities.upgraded.singleInvite.SingleAudienceInvite;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.common.Invite;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.GenericAudience;
import com.edushare.common.vo.Users;

public class PeoplePanel extends EdusharePanel {
	private static final long serialVersionUID = 3143077331392616203L;
	private IModel<?> model;
	private MainInviter inviter;
	private String inviteInput = "";
	private HashMap<String, Integer> map;
	private Integer role = null;
	private SingleAudienceInvite singleAudience = null;
	private MultipleAudienceInvite multipleAudience = null;
	private List<String> statusMessages;
	private boolean check = false;
	private EduShareBasePage nextPage = null;

	public PeoplePanel(String id, EduShareBasePage page, IModel<?> model) {
		super(id, page);
		this.model = model;
		singleAudience = InviteFactory.getInviteClass(model);
		multipleAudience = InviteFactory.getMultipleAudienceInvite(model);
		add(CSSPackageResource
				.getHeaderContribution(Invite.class, "Invite.css"));
		inviter = new MainInviter(getORMInterface());
		Form<Void> f = new Form<Void>("inviteForm") {
			private static final long serialVersionUID = -6692779897671719117L;

			@Override
			public void onSubmit() {
				if (getChoices().size() > 0 && role == null) {
					setError("Please select a role.");
					return;
				}
				if(!StringUtil.isValid(getInviteInput())){
					setError("Please select people/groups to invite");
					return;
				}
				
				inviter.setString(getInviteInput());
				int r = 0;
				if(role!= null)
					r = role;
				if (singleAudience != null) {
					statusMessages = singleAudience.inviteUsers(r, inviter
							.getSingleAudienceList(), getORMInterface());
					statusMessages.addAll(singleAudience.inviteUsers(r,
							inviter.getUnidentifiedUsers(), getORMInterface()));
				} else {
					statusMessages = multipleAudience.inviteUsers(r, check,
							inviter.getAudienceLists());
				}
				setInviteInput("");
				
			}
		};
		
		f.add(new Button("done"){
			private static final long serialVersionUID = -6114027208202821588L;
			@Override
			public void onSubmit(){
				if(getNextPage() != null)
					setResponsePage(getNextPage());
			}
		}.setDefaultFormProcessing(false));
		
		add(f);
		f.add(new EdushareTextArea("inviteInputArea", new PropertyModel<String>(
				this, "inviteInput")));
		WebMarkupContainer wmc = new WebMarkupContainer("checkboxContainer"){
			private static final long serialVersionUID = -5294299333015967447L;
			@Override
			public boolean isVisible(){
				return multipleAudience != null;
			}
		};
		f.add(wmc);
		wmc.add(new CheckBox("checkbox", new PropertyModel<Boolean>(this,
				"check")));
		wmc.add(new Label("choice", new Model<String>(){
			private static final long serialVersionUID = -4337324815105636848L;
			@Override
			public String getObject(){
				if(multipleAudience != null){
					return multipleAudience.getExtraOptions().get(0);
				}
				return "";
			}
		}));

		ListView<String> lv = new ListView<String>("InviteeLV",
				new IModel<List<String>>() {
					private static final long serialVersionUID = 6363393757293375571L;

					public List<String> getObject() {
						if (statusMessages != null)
							return statusMessages;
						else
							return new ArrayList<String>();
					}

					public void setObject(List<String> arg0) {
					}

					public void detach() {
					}
				}) {
			private static final long serialVersionUID = 1359418064959522051L;

			@Override
			protected void populateItem(ListItem<String> arg0) {
				arg0.add(new Label("invitee", arg0.getModelObject()));
			}
		};
		WebMarkupContainer wmc2 = new WebMarkupContainer("inviteContainer"){
			@Override
			public boolean isVisible(){
				return statusMessages!= null && statusMessages.size()>0;
			}
			private static final long serialVersionUID = 3427001612093051526L;
		};
		add(wmc2);
		wmc2.add(lv);
		
		DropDownChoice<String> ddc = new DropDownChoice<String>("roleSelect",
				new PropertyModel<String>(this, "currentRole"), getChoices(),
				new RoleRenderer(map));
		f.add(ddc);
		ddc.setVisible(getChoices().size()>0);
	}

	public String getCurrentRole() {
		if (role == null)
			return null;
		Set<String> set = map.keySet();
		for (String s : set) {
			if (map.get(s).equals(role))
				return s;
		}
		return null;
	}

	public void setCurrentRole(String s) {
		role = map.get(s);
	}

	public List<String> getChoices() {
		this.map = InviteFactory.getRoles(model);
		List<String> strList = new ArrayList<String>();
		Set<Entry<String, Integer>> set = map.entrySet();
		for (Entry<String, Integer> e : set) {
			strList.add(e.getKey());
		}
		return strList;
	}

	public CayenneList<Users> getAllUsers() {
		return inviter.getAllSingleUsers();
	}

	public CayenneList<GenericAudience> getAllAudiences() {
		return null;
	}

	public void setInviteInput(String inviteInput) {
		this.inviteInput = inviteInput;
	}

	public String getInviteInput() {
		return inviteInput;
	}

	public void setCheck(boolean check) {
		this.check = check;
	}

	public boolean getCheck() {
		return check;
	}

	public void setNextPage(EduShareBasePage nextPage) {
		this.nextPage = nextPage;
	}

	public EduShareBasePage getNextPage() {
		return nextPage;
	}

}
